package com.fast.server.component;


import cn.hutool.core.util.ObjUtil;
import com.fast.server.config.downloadfile.DownloadFileConfig;
import com.fast.server.config.toemail.RegisterToSendEmails;
import com.fast.server.config.userspace.UserSpaceConfig;
import com.fast.server.entity.vo.SysUser;
import com.fast.server.entity.constants.Constants;
import com.fast.server.mapper.SysFileInfoMapper;
import com.fast.server.mapper.SysUserMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("redisComponent")
public class RedisComponent {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysFileInfoMapper fileInfoMapper;

    /**
     * 获取系统设置
     *
     * @return
     */
    public RegisterToSendEmails getRegisterToSendEmails() {
        RegisterToSendEmails registerToSendEmails = (RegisterToSendEmails) redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
        if (ObjUtil.isEmpty(registerToSendEmails)) {
            registerToSendEmails = new RegisterToSendEmails();
            redisUtils.set(Constants.REDIS_KEY_SYS_SETTING, registerToSendEmails);
        }
        return registerToSendEmails;
    }

    /**
     * 保存设置
     *
     * @param registerToSendEmails
     */
    public void saveSysSettingsDto(RegisterToSendEmails registerToSendEmails) {
        redisUtils.set(Constants.REDIS_KEY_SYS_SETTING, registerToSendEmails);
    }

    public void saveDownloadCode(String code, DownloadFileConfig downloadFileConfig) {
        redisUtils.setex(Constants.REDIS_KEY_DOWNLOAD + code, downloadFileConfig, Constants.REDIS_KEY_EXPIRES_FIVE_MIN);
    }

    public DownloadFileConfig getDownloadCode(String code) {
        return (DownloadFileConfig) redisUtils.get(Constants.REDIS_KEY_DOWNLOAD + code);
    }


    /**
     * 获取用户使用的空间
     *
     * @param userId
     * @return
     */
    public UserSpaceConfig getUserSpaceUse(String userId) {
        UserSpaceConfig userSpaceConfig = (UserSpaceConfig) redisUtils.get(Constants.REDIS_KEY_USER_SPACE_USE + userId);
        if (ObjUtil.isEmpty(userSpaceConfig)) {
            userSpaceConfig = new UserSpaceConfig();
            Long useSpace = fileInfoMapper.selectUseSpace(userId);
            userSpaceConfig.setUseSpace(useSpace);
            userSpaceConfig.setTotalSpace(getRegisterToSendEmails().getUserInitUseSpace() * Constants.MB);
            redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId, userSpaceConfig, Constants.REDIS_KEY_EXPIRES_DAY);
        }
        return userSpaceConfig;
    }

    /**
     * 保存已使用的空间
     *
     * @param userId
     */
    public void saveUserSpaceUse(String userId, UserSpaceConfig userSpaceConfig) {
        redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId, userSpaceConfig, Constants.REDIS_KEY_EXPIRES_DAY);
    }

    public UserSpaceConfig resetUserSpaceUse(String userId) {
        UserSpaceConfig userSpaceConfig = new UserSpaceConfig();
        Long useSpace = fileInfoMapper.selectUseSpace(userId);
        userSpaceConfig.setUseSpace(useSpace);
        SysUser sysUser = userMapper.selectById(userId);
        userSpaceConfig.setTotalSpace(sysUser.getTotalSpace());
        redisUtils.setex(Constants.REDIS_KEY_USER_SPACE_USE + userId, userSpaceConfig, Constants.REDIS_KEY_EXPIRES_DAY);
        return userSpaceConfig;
    }

    //保存文件临时大小
    public void saveFileTempSize(String userId, String fileId, Long fileSize) {
        Long currentSize = getFileTempSize(userId, fileId);
        redisUtils.setex(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId, currentSize + fileSize, Constants.REDIS_KEY_EXPIRES_ONE_HOUR);
    }

    public Long getFileTempSize(String userId, String fileId) {
        return getFileSizeFromRedis(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + fileId);
    }

    private Long getFileSizeFromRedis(String key) {
        Object sizeObj = redisUtils.get(key);
        if (ObjUtil.isEmpty(sizeObj)) {
            return 0L;
        }
        if (sizeObj instanceof Integer) {
            return ((Integer) sizeObj).longValue();
        } else if (sizeObj instanceof Long) {
            return (Long) sizeObj;
        }
        return 0L;
    }
}
